// JavaScript Document
$.fn.marquee=function(params){
	var options={
		speed:1,	//定时器时间
		add:1,		//单位时间改变量
		orientation:"bottom", //滚动方向，可取：top,right,bottom,left 之一
		defWidth:100,	//控件的默认宽度，当检测出控件的宽度为不大于0时，默认值有效
		defHeight:100	//控件的默认高度，当检测出控件的高度为不大于0时，默认值有效
	}
    if(params!=null){
        $.extend(options,params);
    }
	function marqueeLeft(control){
		var marginLeft=parseInt($(".marquee_item:first",control).css("marginLeft"));
		var width=parseInt($(".marquee_item:first",control).width());
		if(Math.abs(marginLeft)<=width){
			marginLeft-=options.add;			
		}
		else{
			marginLeft+=width;			
		}
		$(".marquee_item:first",control).css("marginLeft",marginLeft);
	}
	function marqueeRight(control){
		var marginLeft=parseInt($(".marquee_item:first",control).css("marginLeft"));
		var width=parseInt($(".marquee_item:first",control).width());
		if(marginLeft>=0){
			marginLeft-=width;			
		}
		else{
			marginLeft+=options.add;			
		}
		$(".marquee_item:first",control).css("marginLeft",marginLeft);
	}
	function marqueeTop(control){
		var marginTop=parseInt($(".marquee_item:first",control).css("marginTop"));
		var height=parseInt($(".marquee_item:first",control).height());
		if(Math.abs(marginTop)<=height){
			marginTop-=options.add;			
		}
		else{
			marginTop+=height;			
		}
		$(".marquee_item:first",control).css("marginTop",marginTop);
	}
	function marqueeBottom(control){
		var marginTop=parseInt($(".marquee_item:first",control).css("marginTop"));
		var height=parseInt($(".marquee_item:first",control).height());
		if(marginTop>=0){
			marginTop-=height;			
		}
		else{
			marginTop+=options.add;			
		}
		$(".marquee_item:first",control).css("marginTop",marginTop);
	}
	function execute(control){
		switch (options.orientation) {
		case "top":
			marqueeTop(control);
			break;
		case "right":
			marqueeRight(control);
			break;
		case "bottom":
			marqueeBottom(control);
			break;
		default:
			marqueeLeft(control);
			break;
		}
	}
	function initLandscape(dom){
		var offset=$(dom).offset();
		var position=$(dom).css("position");
		var width=parseInt($(dom).width());
		if(width<=0) width=options.defWidth;
		var height=parseInt($(dom).height());
		if(height<=0) width=options,defHeight();
		var outside=document.createElement("div");
		$(outside).css({position:position,top:offset.top,left:offset.left,height:height,width:width,overflow:"hidden"});
		var inside=document.createElement("div");
		$(inside).css({height:"100%",width:2*width});
		var item1=$(dom).clone(true).css("float","left").addClass("marquee_item");
		var item2=$(dom).clone(true).css("float","left").addClass("marquee_item");
		$(inside).append(item1);
		$(inside).append(item2);
		$(outside).append(inside);
		$(dom).replaceWith(outside);
		return outside;
	}
	function initPortrait(dom){
		var offset=$(dom).offset();
		var position=$(dom).css("position");
		var width=parseInt($(dom).width());
		if(width<=0) width=options.defWidth;
		var height=parseInt($(dom).height());
		if(height<=0) width=options,defHeight();
		var outside=document.createElement("div");
		$(outside).css({position:position,top:offset.top,left:offset.left,height:height,width:width,overflow:"hidden"});
		var inside=document.createElement("div");
		$(inside).css({height:2*height,width:"100%"});
		var item1=$(dom).clone(true).css("float","top").addClass("marquee_item");
		var item2=$(dom).clone(true).css("float","top").addClass("marquee_item");
		$(inside).append(item1);
		$(inside).append(item2);
		$(outside).append(inside);
		$(dom).replaceWith(outside);
		return outside;
	}
	return this.each(function(){
		var outside;
		switch (options.orientation) {
		case "top":
		case "bottom":
			outside=initPortrait(this);
			break;		
		default:
			outside=initLandscape(this);
			break;
		}
		var timer=setInterval(function(){execute(outside)},options.speed);
		$(outside).hover(function(){
			clearInterval(timer)
		},
		function(){
			timer=setInterval(function(){execute(outside)},options.speed);
		});
	})
}
$(function(){$(".marquee").marquee();})